package com.mei.demo.interceptor;

import java.util.Date;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class InterceptorDemo implements HandlerInterceptor {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {

        System.out.println("拦截器操作......进入 preHandle");

        // 所有请求第一个进入的方法  
        String reqURL = httpServletRequest.getRequestURL().toString();
        String ip = httpServletRequest.getRemoteHost();

        StringBuilder responseStrBuilder = new StringBuilder();
        Enumeration<String> keys = httpServletRequest.getParameterNames();
        while (keys.hasMoreElements()) {
            String k = keys.nextElement();
            responseStrBuilder.append(k).append("=").append(httpServletRequest.getParameter(k)).append(",");
        }

        String parmeter = responseStrBuilder.toString();

        long startTime = System.currentTimeMillis();
        httpServletRequest.setAttribute("startTime", startTime);
        if (handler instanceof HandlerMethod) {
            StringBuilder sb = new StringBuilder(1000);
            sb.append("-----------------------").append(new Date()).append("-------------------------------------\n");
            HandlerMethod h = (HandlerMethod) handler;
            // Controller 的包名  
            sb.append("Controller       : ").append(h.getBean().getClass().getName()).append("\n");
            // 方法名称  
            sb.append("Method           : ").append(h.getMethod().getName()).append("\n");
            // 请求方式  post\put\get 等等  
            sb.append("RequestMethod    : ").append(httpServletRequest.getMethod()).append("\n");
            // 所有的请求参数  
            sb.append("Params           : ").append(parmeter).append("\n");
            // 部分请求链接  
            sb.append("URI              : ").append(httpServletRequest.getRequestURI()).append("\n");
            // 完整的请求链接  
            sb.append("AllURI           : ").append(reqURL).append("\n");
            // 请求方的 ip地址  
            sb.append("request IP       : ").append(ip);

            logger.info(sb.toString());

        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("拦截器操作......进入 postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("拦截器操作......进入 afterCompletion");
    }

}